package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.Comparators;
import net.huashitong.Indicators.utils.ComparatorsUtils;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;

@Service
public class GaugeIndustryService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	
	/**
	 * 规上工业全市各县区排名情况
	 * @param filter
	 * @return
	 */
	public IndicatorBreakdownDatabean queryCountyRank(
			IndicatorBreakdownFilter filter) {
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryByCuMonthAndAreaOR(filter);
	    if(lists.size()<3){
			return null;
		}
		DecimalFormat df=new DecimalFormat("##.##");
		IndicatorBreakdownDatabean databeans2 = new IndicatorBreakdownDatabean();
		int x =1;
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			if(StringUtils.isBlank(indicatorBreakdown.getCumulative())||"0".equals(indicatorBreakdown.getCumulative())){
				databean.setCumulative("0");
			}else{
				String val = ("".equals(indicatorBreakdown.getCumulative())||indicatorBreakdown==null)?"0.00":indicatorBreakdown.getCumulative();
				val =DataUtils.formaterDecimal(val,2);

				databean.setCumulative((Double.parseDouble(val)/10000)+"");
			}
			System.out.println("Cumulative2="+databean.getCumulative());
		
			if(indicatorBreakdown.getArea().contains("福州市")){
				//databeans.add(0, databean);
				databean.setIndex(0);
			}else if(indicatorBreakdown.getArea().contains("永泰")){
				String st=df.format(Double.parseDouble(indicatorBreakdown.getExpectedTarget())/10000);
				databeans2.setExpectedTarget(st);
				databeans2.setArea(indicatorBreakdown.getArea());
				//databeans.add(2, databean);
				databean.setIndex(2);
			}else if(!indicatorBreakdown.getArea().contains("武夷山")&&!indicatorBreakdown.getArea().contains("洛江")){
				//databeans.add(databean);
				databean.setIndex(x);
				x=x+2;
			}
				databeans.add(databean);
		}
		Collections.sort(databeans, new Comparator<IndicatorBreakdownDatabean>() {
			@Override
			public int compare(IndicatorBreakdownDatabean o1, IndicatorBreakdownDatabean o2) {
				if(o1.getIndex()>o2.getIndex()){
					return 1;
				}else if(o1.getIndex()==o2.getIndex()){
					return  0;
				}else {
					return -1;
				}

			}
		});
		databeans2.setRightdata(databeans);
		return databeans2;
	}
	/**
	 * 规模以上工业增加值当月同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists2 = breakdownService.queryMonthDevelopmentSituation(filter);
		//校验
		if(lists2.size()<2){
			return null;
		}
		TertiaryIndustry industries = industryService.queryByfilter(filter);
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		String lastDateVal = "";
		String DateVal = "";
		for (IndicatorBreakdown indicatorBreakdown : lists2) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
			}
			
		}
		Double double1 = Double.parseDouble(lastDateVal);
		Double double2 = Double.parseDouble(DateVal);
		Double double3 = double2-double1;
		//查找去年当季和去年当月前一个月数据
		filter.setCurrentMonth(DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(),-12));
		List<IndicatorBreakdown> indicatorBreakdowns = breakdownService.queryMonthDevelopmentSituation(filter);
		for (IndicatorBreakdown indicatorBreakdown : indicatorBreakdowns) {
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
			}
		}
		double1 = Double.parseDouble(lastDateVal);
		double2 = Double.parseDouble(DateVal);
		Double double4 = double2-double1;
		double3 =(double3-double4)/double4*100;
		databean.setCountyIncrease(industries.getCountyIncrease());
		databean.setCityIncrease(industries.getCityIncrease());
		if(double3 != 0){
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(double3);
			databean.setConcurrentGrowth(st);
		}
		databean.setuBIncrease(industries.getuBIncrease());
		breakdownDatabean2s.add(databean);
		return breakdownDatabean2s;
	}
	/**
	 * 规模以上工业增加值各月绝对值及同比增长情况
	 * @param filter
	 * @return
	 * @throws NumberFormatException
	 * @throws ParseException
	 */
	public List<Map<String, Object>> queryAbsoluteValue(
			IndicatorBreakdownFilter filter) throws NumberFormatException, ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		List<IndicatorBreakdown> breakdowns = breakdownService.queryMonthAbsolute(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryMonthAbsolute(filter);
		breakdownDatabeans = DataUtils.detailData(breakdownSs,breakdownDatabeans);
		List<IndicatorBreakdownDatabean> breakdownDatabeans2 = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans3 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			for (IndicatorBreakdownDatabean databean2 : breakdownDatabeans) {
				if(databean2.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(breakdownDatabean.getCurrentMonth(),-12))){
					Double double5 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double6 = Double.parseDouble(databean2.getSeasonsComparison());
					Double double7 = (double5-double6)/double6*100;
					if(double7 > 0){
						DecimalFormat df=new DecimalFormat(".##");
						String st=df.format(double7);
						breakdownDatabean.setConcurrentGrowth(st);
					}else {
						breakdownDatabean.setConcurrentGrowth(Double.toString(double7));
					}
				}
			}
			Double double7 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(double7);
			breakdownDatabean.setSeasonsComparison(st);
			if(breakdownDatabean.getCurrentMonth().contains("2016")){
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans2.add(breakdownDatabean);
			}else {
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans3.add(breakdownDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabeans2);
		Collections.reverse(breakdownDatabeans3);
		map2.put("lastYear", breakdownDatabeans2);
		map2.put("years", breakdownDatabeans3);
		list.add(map2);
		return list;
	}
	/**
	 * 规模以上工业增加值累计绝对值及同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	public List<Map<String, Object>> queryCumulativeGrowth(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		//累计比增同比增长情况
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdown3s = breakdownService.queryMonthAbsolute(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabean3s = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabean3s = DataUtils.detailcumulativeData(breakdown3s,breakdownDatabean3s);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdown3Ss = breakdownService.queryMonthAbsolute(filter);
		breakdownDatabean3s = DataUtils.detailcumulativeData(breakdown3Ss,breakdownDatabean3s);
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		DecimalFormat df=new DecimalFormat("#.##");
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabean3s) {
			indicatorBreakdown.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			if(indicatorBreakdown.getCurrentMonth().contains(filter.getDate().substring(0,4))){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		String st=df.format(Double.parseDouble(breakdown3s.get(0).getExpectedTarget())/10000);
		map32.put("expectedTarget", st);
		list32.add(map32);
		return list32;
	}
	public List<CompletionClassificationDatabean> queryByXZValue(
			IndicatorBreakdownFilter filter) {
		List<CompletionClassification> classification2s = classificationService.queryByXZValue(filter);
		if(ObjectUtils.isBlank(classification2s)){
			return null;
		}
		List<CompletionClassificationDatabean> classificationDatabean2s = new ArrayList<CompletionClassificationDatabean>(); 
		for (CompletionClassification completionClassification : classification2s) {
			CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			classification.setCategoryDetails(completionClassification.getCategoryDetails());
			classification.setSysId(completionClassification.getSysId());
			classification.setWarningContents(""+completionClassification.getWarningContents());
			Double doublebs1 = Double.parseDouble(completionClassification.getActualCompletionValue());
			Double doublebs2 = Double.parseDouble(completionClassification.getPlanneValue());
			classification.setActualCompletionValue(Double.toString(doublebs1/10000).substring(0, 4));
			classification.setPlanneValue(Double.toString(doublebs2/10000));
			Double doublebs3 = doublebs1/doublebs2*100;
			DecimalFormat df=new DecimalFormat("#.##");
//			if(doublebs3<41){
//				classification.setYujz("0");
//			}else {
//				classification.setYujz("1");
//			}
			String st=df.format(doublebs3);


			if("0".equals(completionClassification.getActualCompletionValue())){
				classification.setCompleteAnnual("0");
			}else {
				classification.setCompleteAnnual(st);
			}

			classificationDatabean2s.add(classification);
		}
		Collections.sort(classificationDatabean2s, new ComparatorsUtils()); 
		return classificationDatabean2s;
	}
	public List<CompletionClassificationDatabean> queryByHYValue(
			IndicatorBreakdownFilter filter) throws ParseException {
		List<CompletionClassification> classification3s = classificationService.queryByHYValueSec(filter);
		if(ObjectUtils.isBlank(classification3s)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextMonthValueDate(filter.getDate(), -1));
		List<CompletionClassification> classifications = classificationService.queryByHYValueSec(filter);
		List<CompletionClassificationDatabean> classificationDatabean3s = new ArrayList<CompletionClassificationDatabean>(); 
		DecimalFormat df=new DecimalFormat("#.##");
		for (CompletionClassification completionClassification : classification3s) {
			for (CompletionClassification completionClassification2 : classifications) {
				if(completionClassification.getCategoryDetails().equals(completionClassification2.getCategoryDetails())){
					CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
					classification.setActualCompletionValue(df.format(Double.parseDouble(completionClassification.getActualCompletionValue())));
					if(StringUtils.isNotBlank(completionClassification.getPlanneValue())){
						classification.setPlanneValue(df.format(Double.parseDouble(completionClassification.getPlanneValue())));
					}
					if(ObjectUtils.isNotBlank(completionClassification2.getActualCompletionValue())){
						classification.setLastActualCompletionValue(df.format(Double.parseDouble(completionClassification2.getActualCompletionValue())));
					}else{
						classification.setLastActualCompletionValue("0");
					}
					classification.setCategoryDetails(completionClassification.getCategoryDetails());
					Double double1 = Double.parseDouble(completionClassification.getSpecificIncrease());
					if(double1 < 0){
						classification.setYujz("0");
					}else {
						classification.setYujz("1");
					}
					classification.setSpecificIncrease(df.format(Double.parseDouble(completionClassification.getSpecificIncrease())));
					classification.setCompletionPeriod(completionClassification.getCompletionPeriod().substring(4));
					classificationDatabean3s.add(classification);
				}
			}
		}
		return classificationDatabean3s;
	}
	public List<CompletionClassificationDatabean> queryByDWValue(
			IndicatorBreakdownFilter filter) throws ParseException {
	
		List<CompletionClassification> classificationds = classificationService.queryByDWValue(filter);
		if(ObjectUtils.isBlank(classificationds)){
			return null;
		}
//		filter.setDate(DateForIndUtils.getNextMonthValueDate(filter.getDate(), -12));
//		List<CompletionClassification> classificationd2s = classificationService.queryByDWValue(filter);
		int k = 0;
		List<CompletionClassificationDatabean> classificationDatabeands = new ArrayList<CompletionClassificationDatabean>(); 
		for (int i = filter.getPage()*20; i < classificationds.size(); i++) {
			if(k == 20){
				break;
			}
//			for (CompletionClassification completionClassification2 : classificationd2s) {
//				if(completionClassification.getCategoryDetails().equals(completionClassification2.getCategoryDetails())){
					CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			        classification.setActualCompletionValue(Double.toString(Double.parseDouble(classificationds.get(i).getActualCompletionValue())));
//					classification.setPlanneValue(classificationds.get(i).getPlanneValue());
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("永泰县", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福建省", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福州市", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福州", ""));
					classification.setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("有限公司", ""));
					DecimalFormat df=new DecimalFormat("#.##");
					String st = df.format(Double.parseDouble(classificationds.get(i).getSpecificIncrease()));
					classification.setSpecificIncrease(st);
//					classification.setCompleteAnnual(df.format(Double.parseDouble(classificationds.get(i).getCompleteAnnual())));
					if(Double.parseDouble(classificationds.get(i).getActualCompletionValue()) == 0 ){
						classification.setLastActualCompletionValue("0");
					}else {
						classification.setLastActualCompletionValue(df.format(Double.parseDouble(classificationds.get(i).getActualCompletionValue())/(1+Double.parseDouble(classificationds.get(i).getSpecificIncrease())/100)));
					}
					classificationDatabeands.add(classification);
					k++;
//				}
//			}
		}
		return classificationDatabeands;
	}

}
